﻿#pragma once
// Copyright (c) lanyeo 2020-2021
// Licenced under MIT license. See https://opensource.org/licenses/MIT for details.
// code style
// 1. use forward declaration (尽可能使用前置声明)
// 2. include file sequence [other project>c>c++>third>self project]
// 

#include <queue>

#include <asio.hpp>

enum NetworkNodeType
{
    NETWORK_NODE_TYPE_MOBILE = 1,
    NETWORK_NODE_TYPE_SERVER = 2,
};

class INetworkManager;

class CNetworkNode
{
public:
    CNetworkNode(INetworkManager* network_manager, asio::io_service* io_service);
    virtual ~ CNetworkNode();

    int SendOneMessage(const std::string& msg);
    int PostRead(char* cache = nullptr, size_t offset = 0);

    const bool& valid();
    void set_valid(bool is_valid);
    const int& type();
    asio::ip::tcp::socket& sock();
    virtual bool canDel() = 0;
    void Destroy();
private:
    char* GetReadCanUseBuf();
    void RecycleReadBuf(char* buf);
    char* GetWriteCanUseBuf();
    void RecycleWriteBuf(char* buf);
private:
    INetworkManager* m_network_manager;
    asio::ip::tcp::socket* m_socket;
    std::queue<char*> m_read_data_buf;
    std::queue<char*> m_write_data_buf;
    bool m_valid;
    int m_type;
};
